package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jx.model.Dep;
import com.jx.service.AddDepService;
import com.jx.servicrimpl.AddDepServiceImpl;
/**
 * 增加部门
 * @author zzzzzzz
 *
 */
public class AddDepController extends HttpServlet{
	AddDepService ads=new AddDepServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		//获取前台数据
		String dname=req.getParameter("dname");
		String ddes=req.getParameter("ddes");
		String[] dfuncs=req.getParameterValues("func");
		String dfunc="";
		//将传回来的部门id拼成一个字符串，以便写入数据库
        for (int i = 0; i < dfuncs.length; i++) {			
			if(i != dfuncs.length-1){
				dfunc+=dfuncs[i]+",";
			}else{
				dfunc+=dfuncs[i];
			}
		}
        //实例化部门
		Dep dep=new Dep(dname,ddes,dfunc);
		//调用增加部门方法
		boolean result=ads.addDepService(dep);
		//判断部门是否增加成功
		String j="0";
		if(result==true){
			j="1";
		}
		//传回数据
		PrintWriter pw=resp.getWriter();
		pw.write(j);
	}
}
